package cn.cms.taglib;

import cn.cms.constants.Constants;
import org.apache.commons.lang.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * Created by leon on 2017/2/17.
 */
public class GetMapValueTag extends TagSupport {

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    private String key;

    private String mapName;

    public String getMapName() {
        return mapName;
    }

    public void setMapName(String mapName) {
        this.mapName = mapName;
    }

    @Override
    public int doStartTag() throws JspException {

        JspWriter jspWriter = this.pageContext.getOut();
        try {

            String out = "";
            if (StringUtils.isNotBlank(key)) {

                if ("SOURCE_ID".equals(mapName)) {

                    out = Constants.SOURCE_ID.get(Integer.valueOf(key));
                } else if ("TYPE_ID".equals(mapName)) {

                    out = Constants.TYPE_ID.get(Integer.valueOf(key));
                } else if ("CATEGORY_ID".equals(mapName)) {

                    out = Constants.CATEGORY_ID.get(Integer.valueOf(key));
                }
            }
            jspWriter.print(out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return super.doStartTag();
    }
}
